package ar.com.santiagobasulto.crawler.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;

import javax.swing.JOptionPane;
import javax.swing.text.html.HTMLEditorKit;

import ar.com.santiagobasulto.crawler.core.URLProcessor;
import ar.com.santiagobasulto.crawler.core.WebCrawlerLogger;

public class EmailFounder extends URLProcessor {
	
	public static ArrayList<String> emailList = new ArrayList<String>(100000);
	
	public EmailFounder(URL url){
		super(url);
	}

	@SuppressWarnings("serial")
	@Override
	public void run() {
		// TODO Auto-generated method stub
		URLConnection connection;
		Collections.synchronizedSet(new HashSet());
        try {
            connection = url.openConnection();
            //TODO check type of connection
            //TODO catch exception to check for
            InputStream in = connection.getInputStream();
            EmailExtractor emailExtractor = new EmailExtractor(in);
            ArrayList<String> aux = emailExtractor.findMails();
            synchronized (emailList) {
            	emailList.addAll(aux);
			}
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            WebCrawlerLogger.log.log(Level.WARNING, "Error procesando la URL: "+url);
        }
	}

}
